/**
 * 广告管理 -列表页面用 JS。
 * Created by bugzy on 2017/5/12 0012.
 */

/**
 * 初始化可编辑项。
 */
auto_edit(".edit");
auto_edit(".img");
auto_edit(".time");
auto_edit(".item");

function search(){
    $("#searchForm").submit();
}

function goodsup(itemId) {
    alertWindow('/goat/goodsup.htm?gid='+itemId, '上款量');
}

/**
 * 广告一定位置
 * @param gid
 * @param direction
 */
function move_goat(gid, direction) {
    gid = gid.trim();
    callapi("/goat/move.json", {gid:gid,move:direction}, function (data) {
        if (data.result ==="success") {
            search();
        } else {
            layui.layer.msg(data.msg);
        }
    })
}

/**
 * 预发布创建
 * @param btn
 * @param gid
 */
function preparePublish(btn, gid) {
    gid = gid.trim();
    var $tr = $(btn).parent().parent();
    $(btn).remove();
    var type = $("#goat_type").val();
    var fromTime = $tr.find('td[data-name="toTime"]').text();
    var tds = $tr.find('td');
    var $subtr = $('<tr></tr>');
    $tr.after($subtr);
    for (var i=0;i<tds.length;i++) {
        var td = $(tds[i]);
        if (i < 2 || i === tds.length - 1) {
            td.attr('rowspan', 2);
        } else if(i === tds.length - 2) {
            var chooseGoods = '';
            if (type == 1) {
                chooseGoods = '<button class="layui-btn layui-btn-mini item-choose" onclick="choose_item(this);">上商品</button>';
            }
            $subtr.append($('<td>' + chooseGoods + '<button class="layui-btn layui-btn-mini" onclick="publish(this, \''+gid+'\', 1);">预发布</button></td>'));
        } else {
            var dataName = td.attr('data-name');
            var text = '';
            if (dataName === 'fromTime') {
                text = fromTime;
            }
            var selector = td.attr('class');
            var cptd = $('<td class="' + selector + '" data-name="' + dataName + '">'+text+'</td>');
            if(selector!==undefined)
            initEdit('.' + selector, '', cptd);
            $subtr.append(cptd);
        }
    }
}

/**
 * 查询广告位置
 */
function locatLocations(){
    var params = $("#searchForm").serialize();
    $.ajax({
        type : "POST",
        url : getRealPath() + '/goat/locations.json',
        data : params,
        dataType : "json",
        async : false,
        success : function(data) {
            var flooroption = '';
            if (data.result === "success") {
                var array = data.data;
                for(var i = 0;i<array.length;i++){
                    flooroption += "<option value='"+array[i].localId+"'>"+array[i].description+"</option>";
                }
                if (flooroption === '') {
                    flooroption = "<option value='' disabled>请选择广告类型</option>";
                }
                $("#goat_location").html(flooroption);
                search();
            } else {
                layui.layer.msg("您在该广告类型中没有位置可选");
                flooroption = "<option value=''>"+"没有位置可选"+"</option>";
            }
            $("#goat_location").html(flooroption);
        }
    });
}

